package com.lsj.spring.config;

import com.lsj.spring.mapper.UserMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import java.io.IOException;
import java.io.InputStream;

/**
 * @author Lsj
 * @version 1.0
 * @date 2021/6/23 20:06
 * 这是一个注解类 configuration
 * ComponentScan("com.lsj.spring")包扫描
 */
@Configuration
@ComponentScan("com.lsj.spring")
public class SpringConfig {
    /**
     * 加载配置文件获得工具类
     * 映射userMapper
     * @return
     * @throws IOException
     */
    @Bean
    public UserMapper userMapper() throws IOException {
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession session = sqlSessionFactory.openSession();
        return session.getMapper(UserMapper.class);
    }
}
